April 10, 2020
숫자 야구 게임
baseball
의 각 행은 [세 자리의 수, 스트라이크의 수, 볼의 수]
를 담고 있습니다.from typing import List
from itertools import permutations
def solution(baseball: List[List[int]]) -> int:
answer = list(permutations(["1", "2", "3", "4", "5", "6", "7", "8", "9"], 3))
for q in baseball:
if q[1] == 3:
"""스트라이크가 3개면 예상 가능한 답은 1개"""
return 1
str_ = str(q[0])
for a in answer[:]:
S = 0
for i in range(3):
"""자리가 같으면 스트라이크"""
if str_[i] == a[i]:
S += 1
"""동일한 숫자의 개수에서 스트라이크 개수를 빼면 볼 개수"""
B = len(set(str_).intersection(a)) - S
if S != q[1] or B != q[2]:
"""스트라이크와 볼 개수가 다르면 알마지 않은 수이므로 제거"""
answer.remove(a)
return len(answer)
import unittest
from .solution import solution
class TestCase(unittest.TestCase):
def test_case_1(self) -> None:
self.assertEqual(
solution([[123, 1, 1], [356, 1, 0], [327, 2, 0], [489, 0, 1]]), 2
)